<?php


namespace app\jiangnan\controller;


use app\jiangnan\config\JNConstants;
use app\service\cxyunService;
use app\utils\Constant;
use app\utils\JwtUtils;
use app\utils\StringUtils;
use think\Controller;
use think\Db;

class Welfare extends Controller
{

    public function getList(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $carList=Db::table("cxyun_car")
            ->where("room_id",$userResult["room_id"])
            ->where("carType",Constant::CAR_WELFARE)
            ->select();
        $roominfo=Db::table("roominfo")
            ->where("id",$userResult["room_id"])
            ->find();
        if(empty($roominfo)){
            return json(["code"=>500,'msg' => '房屋信息错误']);
        }
        foreach ($carList as &$value){
            $value["empMoblie"]=StringUtils::hidePhoneNumber($value["empMoblie"]);
        }
        $roominfo["address"]=$roominfo["community_name"].$roominfo["group"].$roominfo["building"]."-".$roominfo["unit"]."-".$roominfo["room"];
        return json(["code"=>0,'msg' => '获取成功',"data"=>$carList,"roominfo"=>$roominfo]);
    }

    public function add(){
        $accessToken=request()->header("accessToken");
        $post=request()->post();

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }

        $data["parkingId"]=JNConstants::PARKING_ID;
        $data["carTypeId"]=JNConstants::CAR_WELFARE;
        $data["carModelId"]=JNConstants::CAR_MODEL_ID;
        $data["empName"]=$post["empName"];
        $data["empMoblie"]=$post["empMobile"];
        $data["plateNumber"]=$post["plateNumber"];
        $data["startTime"]=date("Y-m-d H:i:s");
        $oneYearLater = strtotime("+1 year");
        $data["endTime"]=date('Y-m-d H:i:s', $oneYearLater);
        

        //查询该该车场是否有该车辆
        //return json(["code"=>1,'msg' => '车牌添加成功',"data"=>$data]);
        $notData["parkingNo"]=JNConstants::PARKING_NO;
        $car=Db::table("cxyun_car")
            ->where("parkingId",$data["parkingId"])
            ->where("plateNumber",$data["plateNumber"])
            ->where("carType",2)
            ->find();
        if(!empty($car)){
            // if($car["carType"]==Constant::CAR_MONTHLY_RENTAL){
            //     return json(["code"=>1,'msg' => '改车牌已经添加为月租车']);
            // }
            return json(["code"=>1,'msg' => '改车牌已经添加']);
        }

        $notData["update_time"]=date("Y-m-d H:i:s");
        $carInfo=array_merge($data,$notData);
        $carInfo["room_id"]=$userInfo["room_id"];
        $carInfo["carType"]=Constant::CAR_WELFARE;

        $re=Db::table("cxyun_car")->insertGetId($carInfo);
        if($re){
            return json(["code"=>0,'msg' => '车牌添加成功']);
        }else{
            return json(["code"=>500,'msg' => "车牌添加失败,请重新添加"]);
        }
    }
    public function add2(){
        $accessToken=request()->header("accessToken");
        $post=request()->post();

        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }

        $data["parkingId"]=JNConstants::PARKING_ID;
        $data["carTypeId"]=JNConstants::CAR_WELFARE;
        $data["carModelId"]=JNConstants::CAR_MODEL_ID;
        $data["empName"]=$post["empName"];
        $data["empMoblie"]=$post["empMobile"];
        $data["plateNumber"]=$post["plateNumber"];
        $data["startTime"]=date("Y-m-d H:i:s");
        $oneYearLater = strtotime("+1 year");
        $data["endTime"]=date('Y-m-d H:i:s', $oneYearLater);
        $data["timestamp"]=time()."000";

        //查询该该车场是否有该车辆
        //return json(["code"=>1,'msg' => '车牌添加成功',"data"=>$data]);
        $notData["parkingNo"]=JNConstants::PARKING_NO;
        $car=Db::table("cxyun_car")
            ->where("parkingId",$data["parkingId"])
            ->where("plateNumber",$data["plateNumber"])
            ->find();
        if(!empty($car)){
            if($car["carType"]==Constant::CAR_MONTHLY_RENTAL){
                return json(["code"=>1,'msg' => '改车牌已经添加为月租车']);
            }
            return json(["code"=>1,'msg' => '改车牌已经添加']);
        }

        $cxyunApi=new cxyunService();
        $res=$cxyunApi->commonPost("saveMonthlyRent",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==1){
            $notData["update_time"]=date("Y-m-d H:i:s");
            $carInfo=array_merge($data,$notData);
            unset($carInfo["timestamp"]);
            $carInfo["room_id"]=$userInfo["room_id"];
            $carInfo["carType"]=Constant::CAR_WELFARE;

            $re=Db::table("cxyun_car")->insertGetId($carInfo);
            if($re){
                return json(["code"=>0,'msg' => '车牌添加成功']);
            }else{
                return json(["code"=>500,'msg' => "车牌添加失败,请重新添加"]);
            }

        }else{
            return json(["code"=>500,'msg' => $result["message"]]);
        }
    }

    public function delete(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $post=request()->post();
        if($userInfo["room_id"]!=$post["room_id"]){
            return json(["code"=>10040,'msg' => '无权操作']);
        }
        $data["parkingId"]=JNConstants::PARKING_ID;
        $data["plateNumber"]=$post["plateNumber"];
        $data["timestamp"]=time()."000";
        $carInfo=Db::table("cxyun_car")->where("id",$post["id"])->find();
        $re=Db::table("cxyun_car")->where("id",$post["id"])->delete();
        $carInfo["delete_name"]=$userInfo["id"];
        $carInfo["delete_time"]=date("Y-m-d H:i:s");
        Db::table("cxyun_car_delete")->insert($carInfo);
        if($re){
            return json(["code"=>0,'msg' => '车牌删除成功']);
        }else{
            return json(["code"=>500,'msg' => "车牌删除失败,请重新操作"]);
        }
    }
    public function delete2(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $post=request()->post();
        if($userInfo["room_id"]!=$post["room_id"]){
            return json(["code"=>10040,'msg' => '无权操作']);
        }
        $data["parkingId"]=JNConstants::PARKING_ID;
        $data["plateNumber"]=$post["plateNumber"];
        $data["timestamp"]=time()."000";
        $notData=array();
        $cxyunApi=new cxyunService();
        $res=$cxyunApi->commonPost("delMonthlyRent",$data,$notData);
        $result= json_decode($res,true);
        if($result["status"]==1){
            $re=Db::table("cxyun_car")->where("id",$post["id"])->delete();
            if($re){
                return json(["code"=>0,'msg' => '车牌删除成功']);
            }else{
                return json(["code"=>500,'msg' => "车牌删除失败,请重新操作"]);
            }

        }else{
            return json(["code"=>500,'msg' => $result["message"]]);
        }
    }

    public function billList(){
        $accessToken=request()->header("accessToken");
        $userResult=JwtUtils::getDecodeToken($accessToken);
        $userInfo=Db::table("userinfo")->where("userid",$userResult["userid"])->find();
        if(empty($userInfo)){
            return json(["code"=>10030,'msg' => '用户未绑定房屋']);
        }
        $dataList=Db::table("cxyun_pay_bill")
            ->where("room_id",$userInfo["room_id"])
            ->where("carType",Constant::CAR_WELFARE)
            ->order("id","desc")
            ->select();
        foreach ($dataList as &$value){
            $record=Db::table("cxyun_entry_exit_record")
                ->where("recordId",$value["recordId"])
                ->find();
            if(!empty($record)){
                if(!empty($record["entranceTime"])){
                    $record["entranceTime"]=date("Y-m-d H:i:s",$record["entranceTime"]/1000);
                }
                if(!empty($record["exitTime"])){
                    $record["exitTime"]=date("Y-m-d H:i:s",$record["exitTime"]/1000);
                    $record["isEit"]=1;
                }
            }
            $value["record"]=$record;
        }
        return json(["code"=>0,'msg' => '获取成功',"data"=>$dataList]);
    }
}